<?php

namespace brand\components;

use brand\page\Table;

class Button extends Component
{

    public function Table($this_column)
    {

        parent::Table($this_column);
        $this_column = $this_column->scopedSlots(
            [
                $this->build($this->config)
            ]
        );

        return $this_column;
    }

    public function build($config = [])
    {

        $button_conf = $config['table']['button_conf'] ?? [];
        if (!$button_conf ) return null;

        $button = Table::$SurfaceTable->button($button_conf['icon'] ?? '', $button_conf['desc'] ?? '');
        $button->props('prop',['type'=>'primary','size'=>'mini2']);
        if (isset($button_conf['actionType'])){

            $method = $button_conf['method'] ?? 'post';
            $data = $button_conf['data'] ?? [];
            $url = $button_conf['url'] ?? '';
            $pk = $button_conf['pk'] ?? 'id';
            $confirm = $button_conf['confirm'] ?? '';

            switch ($button_conf['actionType']) {
                case 'submit':
                    $button->createSubmit([
                        'method'  =>  $method,
                        'data'  =>  $data,
                        'url'  =>  $url,
                    ],$confirm,$pk);
                    break;
                case 'refresh':
                    $button->createRefresh();
                    break;
                case 'page':
                    $button->createPage($url,$data);
                    break;
                case 'confirm':
                    $button->createConfirm($confirm,[
                        'method'  =>  $method,
                        'data'  =>  $data,
                        'url'  =>  $url,
                    ]);
                    break;
                case 'export':
                    $button->props(['url'=>$url,'handler'=>'export']);
                    break;

                case 'createSearch':
                    $button->createSearch();
                    break;
                default:
                    $button->props('handler',$button_conf['actionType']);
                    break;

            }

        }

        $button->props('doneRefresh',true);
        if (isset($button_conf['visible']))$button->visible($button_conf['visible']);
        if (isset($button_conf['title']))$button->props('text',$button_conf['title']);
        if (isset($button_conf['props']))$button->props($button_conf['props']);

        return $button;
    }

    public function buildButtons($arr = [])
    {
        $res = [];
        foreach ($arr as $item) {
            $res[] = $this->build($item);
        }
        return $res;
    }





}